package com.example.aigc_education.controller;


import com.example.aigc_education.domain.ChatHistory;
import com.example.aigc_education.domain.KgChatConversation;
import com.example.aigc_education.domain.Notice;
import com.example.aigc_education.domain.vo.ChatConversationVO;
import com.example.aigc_education.domain.vo.ChatHistoryVO;
import com.example.aigc_education.integration.request.ChatMessageRequestV2;
import com.example.aigc_education.service.ChatHistoryService;
import com.example.aigc_education.service.KgChatConversationService;
import com.example.aigc_education.utils.ResultUtil;
import com.example.aigc_education.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RequestMapping("/chatHistory")
@RestController
public class ChatHistoryController {

    @Resource
    private ChatHistoryService chatHistoryService;

    @Resource
    private KgChatConversationService kgChatConversationService;

    /**
     * 展示AI导师的聊天记录
     */
    @GetMapping("/ai_tutor/chatHistory/{botId}")
    public ResultUtil showAiTutorChatHistory(@PathVariable String botId) {
        List<ChatHistoryVO> chatHistoryVOList = chatHistoryService.showAiTutorChatHistory(botId);
        return ResultUtil.success("历史记录查询成功", chatHistoryVOList);
    }

    /**
     * 展示AILA - AI Learning Assistant(伴学助手)的历史聊天记录列表
     */
    @GetMapping("/AILA/chatHistory/list")
    public ResultUtil getAILAChatHistoryList() {
        Long userId = SecurityUtils.getUserId();
        List<ChatConversationVO> chatConversationList = kgChatConversationService.getUserChatHistoryList(userId);
        return ResultUtil.success("历史记录列表查询成功", chatConversationList);
    }

    /**
     * 展示AILA - AI Learning Assistant(伴学助手)的历史聊天记录
     */
    @GetMapping("/AILA/chatHistory/{convId}")
    public ResultUtil getAILAChatHistory(@PathVariable Long convId) {
        Long userId = SecurityUtils.getUserId();
        List<ChatMessageRequestV2> chatConversationList = kgChatConversationService.getAILAChatHistory(userId,convId);
        return ResultUtil.success("历史记录查询成功", chatConversationList);
    }

    /**
     * 批量删除AILA - AI Learning Assistant(伴学助手)的历史聊天记录
     */
    @PutMapping("/delete")
    public ResultUtil deleteAILAChatHistory(@RequestBody List<Long> convIds) {
        kgChatConversationService.deleteChatHistory(convIds);
        return ResultUtil.success();
    }

    /**
     * 清空AILA - AI Learning Assistant(伴学助手)的历史聊天记录
     */
    @PutMapping("/clear")
    public ResultUtil clearAILAChatHistory() {
        Long userId = SecurityUtils.getUserId();
        Boolean clear = kgChatConversationService.clearChatHistory(userId);
        if (!clear) {
            return ResultUtil.error("清空历史记录失败");
        }
        return ResultUtil.success();
    }

}
